package org.zjx.resolution;

import org.zjx.resource.GameRes;
import org.zjx.resource.IBaseConfig;

import java.util.HashMap;
import java.util.Map;

public class ResolutionManager {
    public static final String WINDOW_TYPE_ROLE = "人物窗口";
    public static final String WINDOW_TYPE_ROLE2 = "人物2窗口";
    public static final String WINDOW_TYPE_PET = "宠物窗口";
    public static final String WINDOW_TYPE_INVENTORY = "道具栏窗口";
    public static final String WINDOW_TYPE_INVENTORY2 = "道具栏2窗口";
    public static final String WINDOW_TYPE_TASK = "任务栏窗口";
    public static final String WINDOW_TYPE_TASK2 = "任务栏2窗口";
    public static final String WINDOW_TYPE_JOB = "任务弹窗窗口";
    public static final String WINDOW_TYPE_MAP = "地图窗口";
    public static final String WINDOW_TYPE_GAME = "游戏窗口";

    private final Map<String, IScreenResolution> windowResolutions = new HashMap<>();

    private static final class InstanceHolder {
        static final ResolutionManager instance = new ResolutionManager();
    }

    public static ResolutionManager getInstance() {
        return InstanceHolder.instance;
    }

    public ResolutionManager() {
        // 初始化代码（如有需要）
    }

    public IScreenResolution loadResolution(IBaseConfig.GameResolution resolution, IBaseConfig.GameTheme theme, String windowType) {
        // 构建resolutionName：theme为null时只使用resolution后缀
        String resolutionName = (theme != null) ?
                String.format("%s_%s", resolution.getSuffix(), theme.getSuffix()) :
                resolution.getSuffix();

        // 构建fileName：theme为null时跳过主题拼接
        String fileName = (theme != null) ?
                String.format("%s_%s_%s", resolution.getSuffix(), theme.getSuffix(), windowType) :
                String.format("%s_%s", resolution.getSuffix(), windowType);

        // 缓存检查
        if (windowResolutions.containsKey(fileName)) {
            return windowResolutions.get(fileName);
        }

        // 加载资源并缓存
        IScreenResolution screenResolution = GameRes.getInstance().loadResource(
                GameRes.RES_WINDOW_SCREEN,
                resolutionName,
                fileName,
                IScreenResolution.class
        );
        windowResolutions.put(fileName, screenResolution);
        return screenResolution;
    }

    public void saveResolution(IBaseConfig.GameResolution resolution, IBaseConfig.GameTheme theme, String windowType, Object object) {
        // 构建resolutionName：theme为null时只使用resolution后缀
        String resolutionName = (theme != null) ?
                String.format("%s_%s", resolution.getSuffix(), theme.getSuffix()) :
                resolution.getSuffix();

        // 构建fileName：theme为null时跳过主题拼接
        String fileName = (theme != null) ?
                String.format("%s_%s_%s", resolution.getSuffix(), theme.getSuffix(), windowType) :
                String.format("%s_%s", resolution.getSuffix(), windowType);

        // 保存资源
        GameRes.getInstance().saveResource(
                object,
                GameRes.RES_WINDOW_SCREEN,
                resolutionName,
                fileName
        );
    }
}